using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Marlimer.Common;
using Marlimer.Common.Entidades;
using System.Data.SqlClient;

namespace Marlimer.Data.Comercial
{
    public class rhh_CalendarioLaboral
    {

        public DataTable MostrarCalendarioByMesAnio(int nanio, int nmes)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            sqlcmd.CommandText = "spusMostrarCalendarioByMesAnio";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@NANIO", SqlDbType.Int);
            sqlcmd.Parameters.Add("@NMES", SqlDbType.Int);

            sqlcmd.Parameters["@NANIO"].Value = nanio;
            sqlcmd.Parameters["@NMES"].Value = nmes;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];

        }

        public resulMsgError guardar(List<InfoCalendarioLaboral> calen)
        {
            resulMsgError resul = new resulMsgError(false, "");
 
            SqlCommand sqlcmd = new SqlCommand();
            //crear el objeto transaccion
            SqlTransaction transac = Session.getConexion().BeginTransaction("CalendarioLaboral");
  
            sqlcmd.CommandText = "spuActualizaDiaCalendario";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();
            sqlcmd.Transaction = transac;
            sqlcmd.Parameters.Add("@dfecha", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@flg_laboral", SqlDbType.Int);
            sqlcmd.Parameters.Add("@flg_feriado", SqlDbType.Int);


            try
            {

                foreach (InfoCalendarioLaboral cal in calen)
                {
                    sqlcmd.Parameters["@dfecha"].Value = cal.dfecha;
                    sqlcmd.Parameters["@flg_laboral"].Value = cal.flg_laboral;
                    sqlcmd.Parameters["@flg_feriado"].Value = cal.flg_feriado;
                    sqlcmd.ExecuteNonQuery();
                }
                transac.Commit();
                resul.resul = true;
            }
            catch (Exception ex)
            {
                //terminar la transaccion
                transac.Rollback();
                resul.message = ex.Message;
            }

            return resul;
        }

    }
}
